import { yunshanfu, orderInList, res_orderDetail } from "./yunshanfu"
import { mainWindow } from "./index";

export class accManager {
    static ins: accManager = null;
    public accList: Map<string, { user: string, pwd: string, ins: yunshanfu }> = new Map();
    constructor() {
        accManager.ins = this;
    }
    deleteAll() {
        this.accList.forEach((v, k) => {
            if (v && v.ins) {
                v.ins.clear();
            }
        })
        this.accList.clear();
    }
    deleteAcount(user: string) {
        let d = this.accList.get(user);
        if (d && d.ins) {
            d.ins.clear();
        }
        this.accList.delete(user);
    }
    getUser(user: string) {
        return this.accList.get(user);
    }
    public async verifySms(user: string, code: string) {
        let u = this.getUser(user);
        if (u) {
            try {
                let isok = await u.ins.verifySms(code, false);
                mainWindow.webContents.send('smsresult', user, isok);
            } catch (e) {
                mainWindow.webContents.send('loginerror', user, e.message);
            }
        }
    }
    public async retryCard(user: string) {
        let u = this.getUser(user);
        if (u) {
            try {
                await u.ins.cardInit();
            } catch (e) {
                mainWindow.webContents.send('cardiniterr', user, e.message);
            }
        }
    }
    public async retrySms(user: string) {
        let u = this.getUser(user);
        if (u) {
            try {
                await u.ins.sendMeSms();
            } catch (e) {

            }
        }
    }
    addAccount(user: string, pwd: string) {
        this.deleteAcount(user);
        let d = new yunshanfu();


        d.on(yunshanfu.EVENT_INITED, (isok: boolean) => {
            this.onInit(user, isok)
        })
        d.on(yunshanfu.EVENT_LOGINSMSNEEDED, () => {
            //输入验证码
            this.onNeedSms(user);

        })
        d.on(yunshanfu.EVENT_LOGINED, () => {
            this.onLogined(user);

        })
        d.on(yunshanfu.EVENT_CARDINITED, () => {
            this.onCardInited(user);
        })
        d.on(yunshanfu.EVENT_NEWORDER, (order: orderInList, detail: res_orderDetail) => {
            this.onNewOrder(user, order, detail);
            //do somethings;
        })

        this.accList.set(user, {
            user: user, pwd: pwd, ins: d
        });
    }
    protected async onInit(user: string, isok: boolean) {
        mainWindow.webContents.send('init', user, isok);
        if (!isok) return;
        let info = this.accList.get(user)
        if (info) {
            try {
                await info.ins.Login(info.user, info.pwd);
            } catch (e) {
                //登录错误返回数据
                mainWindow.webContents.send('loginerror', user, e.message);
            }

        }

    }
    protected onNeedSms(user: string) {
        //d.verifySms("123123", true);
        mainWindow.webContents.send('sms', user);
    }
    protected async onLogined(user: string) {

        let info = this.accList.get(user)
        if (info) {
            mainWindow.webContents.send('loginok', user, info.ins.userInfo);
            try {
                await info.ins.cardInit();
            } catch (e) {
                mainWindow.webContents.send('cardiniterr', user, e.message);
            }

        }
    }
    protected async onCardInited(user: string) {
        //dosomething
        let info = this.accList.get(user)
        if (info) {
            mainWindow.webContents.send('cardinitok', user, info.ins.cardInfo, await info.ins.createQrcode());
        }
    }
    protected onNewOrder(user: string, order: orderInList, detail: res_orderDetail) {

        let orderdata: {
            orderId: string;
            time: string;
            amount: string;
            currency: string;
            reciveCard: string;
            payInfo: string;
            desc: string;
        } = {
            orderId: order.orderId,
            time: detail ? detail.orderTime : order.orderTime,
            amount: (detail ? (parseInt(detail.totalAmount) / 100).toFixed(2) : order.amount),
            currency: order.currencyUnit,
            reciveCard: detail ? detail.orderDetail.collectionCardInfo : "",
            payInfo: detail ? (detail.orderDetail.payUserName + "-" + detail.orderDetail.payCardInfo) : order.title,
            desc: detail ? detail.orderDetail.postScript : ""
        }
        mainWindow.webContents.send('neworder', user, orderdata);
    }

}
new accManager();